package com.cwj.utils;

import com.cwj.UnicomException;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

/**
 * @author wujie.chen
 * 2021/12/9 11:22
 */
public class FileUtils {
    public static void m1(String src, String target) throws IOException {
        File file = new File(src);
        if(!file.exists()){
            throw new RuntimeException("file " + src + " is not exists");
        }
        FileInputStream inputStream = new FileInputStream(file);
        FileChannel channel = inputStream.getChannel();
        FileChannel targetChannel = new FileOutputStream(target).getChannel();
        long length = file.length();
        long l = channel.transferTo(0, file.length(), targetChannel);
        // 实际
        System.out.println("should transfer: " + length + "; actual: " + l);
    }

    public static void m2(String src, String target){
        File file = new File(src);
        if(!file.exists()){
            throw new RuntimeException("file " + src + "is not exists");
        }
        try(FileInputStream inputStream = new FileInputStream(file);
            FileOutputStream targetOutputStream = new FileOutputStream(target)){
            byte[] data = new byte[1024];
            int l = 0;
            while ((l = inputStream.read(data)) != -1){
                targetOutputStream.write(data);
            }
        }catch (IOException e){
            System.out.println(e);
        }

    }
    public static String fileSizeText(long size){
        if(size<0){
            throw new RuntimeException("size < 0");
        }
        if(size==0){
            return "0 Byte";
        }
        StringBuilder sb = new StringBuilder();
        long bt = size & 0x3ff;
        if(bt>0){
            sb.insert(0, bt + "Byte");
        }
        long kb = (size & (0x3ff << 10)) >> 10;
        if(kb > 0){
            sb.insert(0, kb + "KB_");
        }
        long mb = (size & (0x3ff << 20)) >> 20;
        if(mb>0){
            sb.insert(0, mb + "MB_");
        }
        long gb = (size & (0x3ffL << 30)) >> 30;
        if(gb>0){
            sb.insert(0, gb + "GB_");
        }
        String s = sb.toString();
        if(s.endsWith("_")){
            s = s.substring(0, s.length()-1);
        }
        return s;
    }

    public static String getFileExtensionName(String fileName) {
        if (fileName != null && fileName.length()>0) {
            int i = fileName.lastIndexOf(".");
            if (i > -1 && i < fileName.length() - 1) {
                return fileName.substring(i+1);
            }
        }
        throw new UnicomException("获取文件扩展名失败 fileName ：" + fileName);
    }


    public static void main(String[] args) throws IOException {
        String src = "D:\\BaiduNetdiskDownload/BaiduNetdiskDownload.rar";
        String target = "D:\\BaiduNetdiskDownload/BaiduNetdiskDownload2.rar";
        long l1 = System.currentTimeMillis();
        m1(src, target);
        long l2 = System.currentTimeMillis();
        m2(src, target);
        long l3 = System.currentTimeMillis();
        System.out.println(l2 -l1);
        System.out.println(l3 -l2);
    }
}
